package org.fuys.coder.domain.act.lottery.model.vo.strategy;

import org.fuys.coder.common.entity.Pair;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.lottery.model.vo.strategy
 * @className: LotteryStrategyRateMap
 * @author: WangJing
 * @description: 用于存储别名数组 从而实现别名权重概率随机算法达到O(1)的效率
 * @date: 2024/7/7 23:42
 * @version: 1.0
 */

public class LotteryStrategyRateMap {

    /**
     * 斐波那契散列增量，逻辑：黄金分割点：(√5 - 1) / 2 = 0.6180339887，Math.pow(2, 32) * 0.6180339887 = 0x61c88647
     */
    public static final int HASH_INCREMENT = 0x61c88647;

    /**
     * 数组初始化长度 128，保证数据填充时不发生碰撞的最小初始化值
     */
    public static final int RATE_TUPLE_LENGTH = 128;

    private static Map<Pair<Integer,Integer>, List<Integer>> tupleMap=new ConcurrentHashMap<>();
    public static void setMapValue(Pair<Integer,Integer> key, List<Integer> value){
        tupleMap.put(key,value);
    }
    public static List<Integer> getMapValue(Pair<Integer,Integer> key){
        return tupleMap.get(key);
    }
    public static int hashIdx(int val) {
        int hashCode = val * HASH_INCREMENT + HASH_INCREMENT;
        return hashCode & (RATE_TUPLE_LENGTH - 1);
    }
}
